package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.ClasseWorkMapper;
import com.indusfo.spc.pojo.ClasseWork;
import com.indusfo.spc.service.ClasseWorkService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ClasseWorkServiceImpl implements ClasseWorkService {

    private  static  final Logger logger = LoggerFactory.getLogger(ClasseWorkServiceImpl.class);

    @Resource
    private ClasseWorkMapper classeWorkMapper;


    /**
     * 编辑
     *
     * @param classeWork
     * @return  JSONObject
     */
    @Override
    public JSONObject updateClasseWork(ClasseWork classeWork) {
        try {
            if(classeWork.getClasseWorkId() == null) {
                throw new ParamsErrorException("请选择要登出的班次开工");
            }
            if(classeWork.getLogoutUserId() == null) {
                throw new ParamsErrorException("请填写登出用户");
            }
//            checkParam(classeWork);
            // 调用存储过程
            int row = classeWorkMapper.updateByPrimaryKeySelective(classeWork);
            if(row == 0) {
                throw new ModifyFailedException("登出失败");
            }
            return JSONObject.oK("登出成功");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /***
     * 查询（分页）
     *
     * @return com.indusfo.spc.vo.JSONObject
     */
    @Override
    public JSONObject selectAll(ClasseWork classeWork) {
        List<ClasseWork> listClasseWork = null;
        try {
            Integer pagesize = classeWork.getPagesize();
            Integer pageindex = classeWork.getPageindex();
            if(pagesize != null && pageindex != null) {
                classeWork.setIncept(pagesize*(pageindex - 1));
            }
            //查询全部
            listClasseWork = classeWorkMapper.selectAll(classeWork);
            if (listClasseWork.isEmpty()) {
                return JSONObject.oK("没有查询到数据", listClasseWork, 0);
            }
            //查询分页总记录数
            int count = classeWorkMapper.countClasseWork(classeWork); //返回查询到的总记录数
            return JSONObject.oK("查询成功", listClasseWork, count);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /***
     * 新增
     *
     * @param classeWork
     * @return com.indusfo.spc.vo.JSONObject
     */
    @Override
    public JSONObject insertClasseWork(ClasseWork classeWork) {
        try {
            if(classeWork.getClasseWorkId() != null) {
                throw new ParamsErrorException("新增时，不能填写id");
            }
            checkParam(classeWork);
            int	row = classeWorkMapper.insertSelective(classeWork);
            if(row == 0) {
                throw new ModifyFailedException("数据新增失败");
            }
            return JSONObject.oK("新增成功");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 1 启用  2 删除  3停用 
     *
     * @param classeWorkIds
     * @param dataState
     * @return com.indusfo.spc.vo.JSONObject
     */
    @Override
    public JSONObject definedClasseWork(Long[] classeWorkIds, Integer dataState) {
        try {
            if(dataState == null) {
                throw new ParamsErrorException("数据状态不能为空");
            }else if(dataState != 1 && dataState != 2 && dataState != 3) {
                throw new ParamsErrorException("数据状态错误");
            }
            if (classeWorkIds == null || classeWorkIds.length == 0) {
                throw new ParamsErrorException("班次开工ID不能为空");
            }
            // 执行存储过程
            int row = classeWorkMapper.definedClasseWork(classeWorkIds, dataState);
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数lDataState,返回相应信息
                switch (dataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                    default:
                }
            } else {
                switch (dataState) {
                    case 1:
                        msg = "启用成功";break;
                    case 2:
                        msg = "删除成功";break;
                    case 3:
                        msg = "停用成功";break;
                    default:
                }
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject logoutClasseWork(Long[] classeWorkIds, Integer logoutUserId) {
        try {
            if(classeWorkIds == null || classeWorkIds.length == 0) {
                throw new ParamsErrorException("请选择要登出的班次开工");
            }
            if(logoutUserId == null || logoutUserId == 0) {
                throw new ParamsErrorException("请填写登出用户");
            }
            // 调用存储过程
            int row = classeWorkMapper.logoutClasseWork(classeWorkIds,logoutUserId);
            if(row == 0) {
                throw new ModifyFailedException("登出失败");
            }
            return JSONObject.oK("登出成功");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject queryClasseHour(Integer classeId) {
        String classeHour = "";
        SimpleDateFormat ymd=new SimpleDateFormat("yyyy-MM-dd");
        String format = ymd.format(new Date());
        try {
            if(classeId == null || classeId == 0){
                throw new ModifyFailedException("请选择班次");
            }
            //查询全部
            classeHour = classeWorkMapper.queryClasseHour(classeId);
            if (classeHour.isEmpty()) {
                return JSONObject.oK("没有查询到数据", format, 0);
            }
            format = format +" "+ classeHour;
            return JSONObject.oK("查询成功", format,0);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 班次开工校验
     * @param classeWork
     */
    public void checkParam(ClasseWork classeWork){
        if (classeWork.getClasseId() == null) {
            throw new ParamsErrorException("请选择班次");
        }
        if (classeWork.getProductlineId() == null) {
            throw new ParamsErrorException("请选择产线");
        }
        // 不能重复
        Integer CodeCounts = classeWorkMapper.queryClasseWork(classeWork);
        if (CodeCounts !=0) {
            throw new ParamsErrorException("该产线下班次未完结");
        }
    }
    
}
